import java.util.Stack;

/**
 * Creat with IntelliJ IDEA
 * Description:
 * User:WZW
 * Date:2022-02-03
 * Time:11:38
 */
/*
844. 比较含退格的字符串
https://leetcode-cn.com/problems/backspace-string-compare/
 */
class Solution {
    public static Stack<Character> put(String s){
        Stack<Character> S = new Stack<>();
        for(int i = 0; i < s.length(); i++)
        {
            if(s.charAt(i) == '#'){
                if(!S.empty()){
                    S.pop();
                }
            }else{
                S.push(s.charAt(i));
            }
        }
        return S;
    }
    public boolean backspaceCompare(String s, String t) {
        Stack<Character> ss = Solution.put(s);
        Stack<Character> tt = Solution.put(t);
        while(!ss.empty() && !tt.empty() && ss.peek() == tt.peek()){
            ss.pop();
            tt.pop();
        }
        return ss.empty() && tt.empty();
    }
}

public class homework {
    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.backspaceCompare("a##c", "#a#c"));
    }

    /*
    682. 棒球比赛
    https://leetcode-cn.com/problems/baseball-game/
     */
    public int calPoints(String[] ops) {
        Stack<Integer> stack = new Stack<>();
        for(int i = 0; i < ops.length; i++){
            if(ops[i].equals("C")){
                stack.pop();
            }
            else if(ops[i].equals("D")){
                stack.push(2*stack.peek());
            }
            else if(ops[i].equals("+")){
                int val = stack.pop();
                int add = stack.peek() + val;
                stack.push(val);
                stack.push(add);
            }
            else{
                stack.push(Integer.valueOf(ops[i]));
            }
        }
        int sum = 0;
        while(!stack.empty()){
            sum+=stack.pop();
        }
        return sum;
    }

    public static void main1(String[] args) {
        myStack stack = new myStack();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.push(5);
        stack.push(6);
        System.out.println(stack.peek());
        System.out.println(stack.isEmpty());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.pop());
        System.out.println(stack.isEmpty());
    }

    public boolean IsPopOrder(int [] pushA,int [] popA) {
        Stack<Integer> stack = new Stack<>();
        int j = 0;//遍历popA
        for (int i = 0; i < pushA.length; i++) {
            stack.push(pushA[i]);
            while (j < popA.length && !stack.isEmpty() && stack.peek()==popA[j]){
                stack.pop();
                j++;
            }
        }
        return stack.isEmpty();
    }

//    boolean isPopOrder(int [] pushA,int [] popA){
//        Stack<Integer> stack = new Stack();
//        int i = 0;
//        int j = 0;
//        while(i<pushA.length && j<popA.length){
//            if(pushA[i]==popA[j]){
//                stack.pop();
//                i++;
//                j++;
//            }else{
//                stack.push(pushA[i]);
//                i++;
//            }
//        }
//        return stack.isEmpty();
//    }

}
